package com.ruoyi.project.system.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.project.system.domain.BookUser;
import com.ruoyi.project.system.domain.TbBook;
import com.ruoyi.project.system.service.IBookUserService;
import com.ruoyi.project.system.service.ITbBookService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TbOrder;
import com.ruoyi.project.system.service.ITbOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2020-03-06
 */
@RestController
@RequestMapping("/system/order")
public class TbOrderController extends BaseController
{
    @Autowired
    private ITbOrderService tbOrderService;

    @Autowired
    private ITbBookService tbBookService;

    @Autowired
    private IBookUserService bookUserService;

    private String a="未发货";

    private String b="发货中";

    private String c="已完成";

    private String d="在线付款";

    private String e="货到付款";

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbOrder tbOrder)
    {
        startPage();
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        if (!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                BookUser bookUser = bookUserService.selectBookUserById(list.get(i).getBookUserId());
                TbBook tbBook = tbBookService.selectTbBookById(list.get(i).getBookId());
                if (bookUser != null) {
                    jsonArray.getJSONObject(i).put("bookUser", bookUser.getUserName());
                }
                if (tbBook != null) {
                    jsonArray.getJSONObject(i).put("bookName", tbBook.getName());
                }
                if (list.get(i).getStatus() != null) {
                    if (list.get(i).getStatus() == -1) {
                        jsonArray.getJSONObject(i).put("istatus", a);
                    } else if (list.get(i).getStatus() == 0) {
                        jsonArray.getJSONObject(i).put("istatus", b);
                    } else if (list.get(i).getStatus() == 1) {
                        jsonArray.getJSONObject(i).put("istatus", c);
                    }
                }
                if (list.get(i).getPayType() != null) {
                    if (list.get(i).getPayType() == 0) {
                        jsonArray.getJSONObject(i).put("type", d);
                    } else if (list.get(i).getPayType() == 1) {
                        jsonArray.getJSONObject(i).put("type", e);
                    }
                }
            }

//        }else {
//            return AjaxResult.error("订单不存在");
//        }
            System.out.println(jsonArray.toJSONString());
        }
        return getDataTable(jsonArray);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbOrder tbOrder)
    {
        List<TbOrder> list = tbOrderService.selectTbOrderList(tbOrder);
        ExcelUtil<TbOrder> util = new ExcelUtil<TbOrder>(TbOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbOrderService.selectTbOrderById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbOrder tbOrder)
    {
        return toAjax(tbOrderService.insertTbOrder(tbOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbOrder tbOrder)
    {

        return toAjax(tbOrderService.updateTbOrder(tbOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbOrderService.deleteTbOrderByIds(ids));
    }
}
